package com.sduept.nwld.mobile.rest;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sduept.core.ParamManager;
import com.sduept.nwld.mobile.manager.FaultReportMobileManager;
import com.sduept.nwld.mobile.manager.MobileUserInfoManager;
import com.sduept.nwld.mobile.manager.MobileUtil;
import com.sduept.nwld.mobile.model.fault.FaultListDTO;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.manager.PersonnelManager;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * Simple to Introduction
 * 
 * @Description: [故障相关服务接口]
 * @Author: [lixz]
 * @CreateDate: [2019年6月25日 下午12:31:23]
 * 
 * 
 */
@Slf4j
@RestController
@RequestMapping("/fault/")
public class FaultReportServices {

	@Autowired
	private FaultReportMobileManager faultReportMobileManager;
	@Autowired
	private MobileUserInfoManager mu;
	@Autowired
	private ParamManager paramCache;
	// SQL注入检测类
	@Inject
	private MobileUtil mobileUtil;
	@Autowired
	private PersonnelManager pm;


	/**
	 * 无账号查询故障列表（获取所有信息数据）
	 * 
	 * @param month
	 * @param currentPage
	 * @return
	 */

	@GetMapping("/getFaultList/{month}/{currentPage}")
	public List<FaultListDTO> getFaultReport(@PathVariable("month") int month,@PathVariable("currentPage") int currentPage) {
		try {
			month = Integer.parseInt(mobileUtil.SQLInjectCheck(String.valueOf(month)));
			currentPage = Integer.parseInt(mobileUtil.SQLInjectCheck(String.valueOf(currentPage)));
		} catch (Exception e) {
			log.error("异常：接口参数检查:SQL注入检查未通过!");
			e.printStackTrace();
			return null;
		}
		List<FaultListDTO> result = faultReportMobileManager.getFaultReportList(month, currentPage, null);
		return result;
	}

	/**
	 * 根据账号获取故障列表（获取有相应权限的信息数据）
	 * 
	 * @param month       月份
	 * @param currentPage 当前页
	 * @param account     账号
	 * @return
	 */

	@GetMapping("/getFaultByAccount/{month}/{currentPage}/{account}")
	public List<FaultListDTO> getFaultByAccount(@PathVariable("month") int month,@PathVariable("currentPage") int currentPage,
			@PathVariable("account") String account) {
		try {
			month = Integer.parseInt(mobileUtil.SQLInjectCheck(String.valueOf(month)));
			currentPage = Integer.parseInt(mobileUtil.SQLInjectCheck(String.valueOf(currentPage)));
			account = mobileUtil.SQLInjectCheck(account);
		} catch (Exception e) {
			log.error("异常：接口参数检查:SQL注入检查未通过!");
			e.printStackTrace();
			return null;
		}
		PersonnelEntityBean person = pm.findPersonnelByCode(account);
		if (person != null) {
			List<FaultListDTO> result = faultReportMobileManager.getFaultReportList(month, currentPage,
					person.getCode());
			return result;
		}else {
			log.error("用户信息:找不到account所对应的用户信息!");
			return null;
		}
	}
	/**
	 * 根据账号获取故障列表（获取有相应权限的信息数据）
	 * 
	 * @param month       月份
	 * @param currentPage 当前页
	 * @param account     账号
	 * @return
	 */

	@GetMapping("/getFaultByAccount1/{month}/{currentPage}/{account}")
	public String getFaultByAccount1(@PathVariable("month") int month,@PathVariable("currentPage") int currentPage,
			@PathVariable("account") String account) {
		try {
			month = Integer.parseInt(mobileUtil.SQLInjectCheck(String.valueOf(month)));
			currentPage = Integer.parseInt(mobileUtil.SQLInjectCheck(String.valueOf(currentPage)));
			account = mobileUtil.SQLInjectCheck(account);
		} catch (Exception e) {
			log.error("异常：接口参数检查:SQL注入检查未通过!");
			e.printStackTrace();
			return null;
		}
		PersonnelEntityBean person = pm.findPersonnelByCode(account);
		if (person != null) {
			List<FaultListDTO> result = faultReportMobileManager.getFaultReportList(month, currentPage,
					person.getCode());
			String json = "";
			try {
//				Jsonb jsonb = JsonbBuilder.create();
//				json = jsonb.toJson(result);
				json = JSONObject.toJSON(result).toString();
			} catch (Exception e) {
				log.error("异常：json转换异常");
				e.printStackTrace();
			}
			return json;
		}else {
			log.error("用户信息:找不到account所对应的用户信息!");
			return null;
		}
	}
	/**
	 * 故障详情
	 * 
	 * @param faultId 故障id
	 * @return
	 */

	@GetMapping("/getFaultDetail/{faultId}")
	public Map<String,Object> getFaultDetail(@PathVariable("faultId") String faultId) {
		try {
			faultId = mobileUtil.SQLInjectCheck(faultId);
		} catch (Exception e) {
			log.error("异常：接口参数检查:SQL注入检查未通过!");
			e.printStackTrace();
			return null;
		}
		Map<String, Object> result = faultReportMobileManager.getFaultReportDetailMap(faultId);
	    return result;
	}

	/**
	 * 根据厂站、设备、开始时间、结束时间、账号搜索故障列表
	 * 
	 * @param jsonPara json字符串，包含5个键值对。
	 * "{stationName:'宝安换流站',
	 *   deviceName:'兴安直流极',
	 *   startTime:'2018-09-16',
	 *   endTime:'2018-09-18',
	 *   account:'',
	 *   currentPage''}"
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")

	@GetMapping("/searchFaultByAccount/{jsonPara}")
	public List<FaultListDTO> searchFaultByAccount(@PathVariable("jsonPara") String jsonPara) {
		Map mapType = null;
		try {
			mapType = JSON.parseObject(jsonPara, Map.class);
		} catch (Exception e) {
			log.error("异常：接口参数检查:参数非json格式!");
			e.printStackTrace();
			return null;
		}
		String stationName = "";
		String deviceName = "";
		String startTime = "";
		String endTime = "";
		String account = "";
		String currentPage = "";
		try {
			account = mapType.get("account").toString();
			stationName = mapType.get("stationName").toString();
			deviceName = mapType.get("deviceName").toString();
			startTime = mapType.get("startTime").toString();
			endTime = mapType.get("endTime").toString();
			account = mapType.get("account").toString();
			currentPage = mapType.get("currentPage").toString();
		} catch (Exception e) {
			log.error("异常：接口参数检查:json参数中缺少所需参数！");
			e.printStackTrace();
			return null;
		}
		try {
			stationName = mobileUtil.SQLInjectCheck(stationName);
			deviceName = mobileUtil.SQLInjectCheck(deviceName);
			startTime = mobileUtil.SQLInjectCheck(startTime);
			endTime = mobileUtil.SQLInjectCheck(endTime);
			account = mobileUtil.SQLInjectCheck(account);
			currentPage = mobileUtil.SQLInjectCheck(currentPage);
		} catch (Exception e) {
			log.error("异常：接口参数检查:SQL注入检查未通过");
			e.printStackTrace();
			return null;
		}
		if(StringUtils.isNotEmpty(account)) {
			PersonnelEntityBean person = pm.findPersonnelByCode(account);
			if (person != null) {
				List<FaultListDTO> result = faultReportMobileManager.getFaultReportListByDateAndDeviceAndStationAndUserCode(
						startTime, endTime, deviceName, stationName, person.getCode(),currentPage);
				return result;
			}else {
				log.error("用户信息:找不到account所对应的用户信息!");
				return null;
			}
		}else {
			//无账号返回
			log.error("接口参数检查:未找到account参数值!");
			return null;
		}
	}

}
